package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.YxShopInfo;
import co.yixiang.modules.shop.service.YxShopInfoService;
import co.yixiang.modules.shop.service.dto.YxShopInfoQueryCriteria;
import co.yixiang.utils.OrderUtil;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Walert
* @date 2020-03-23
*/
@Api(tags = "店铺信息接口API管理")
@RestController
@RequestMapping("/api/yxShopInfo")
public class YxShopInfoController {

    private final YxShopInfoService yxShopInfoService;

    public YxShopInfoController(YxShopInfoService yxShopInfoService) {
        this.yxShopInfoService = yxShopInfoService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','yxShopInfo:list')")
    public void download(HttpServletResponse response, YxShopInfoQueryCriteria criteria) throws IOException {
        yxShopInfoService.download(yxShopInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询店铺信息接口API")
    @ApiOperation("查询店铺信息接口API")
    @PreAuthorize("@el.check('admin','yxShopInfo:list')")
    public ResponseEntity<Object> getYxShopInfos(YxShopInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxShopInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增店铺信息接口API")
    @ApiOperation("新增店铺信息接口API")
    @PreAuthorize("@el.check('admin','yxShopInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxShopInfo resources){
        resources.setAddTime(OrderUtil.getSecondTimestampTwo());
        return new ResponseEntity<>(yxShopInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改店铺信息接口API")
    @ApiOperation("修改店铺信息接口API")
    @PreAuthorize("@el.check('admin','yxShopInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxShopInfo resources){
        resources.setChangeTime(OrderUtil.getSecondTimestampTwo());
        yxShopInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除店铺信息接口API")
    @ApiOperation("删除店铺信息接口API")
    @PreAuthorize("@el.check('admin','yxShopInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        yxShopInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}